package com.kiven.thread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadLoclDemo {
	private final Lock lock = new ReentrantLock();
	private final Condition notFull = lock.newCondition();
	private final Condition notEmpty = lock.newCondition();
	private final Object[] items = new Object[100];
	private int putptr,takeptr,count;
	
	
	public void put(Object o) throws InterruptedException {
		lock.lock();
		try {
			while(count == items.length)
				notFull.await();
			items[putptr] = 0;
			if (++putptr == items.length) putptr = 0;
			++count;
			notEmpty.signal();
			} finally {
				lock.unlock();
			}
	}
	
	public Object get() throws InterruptedException {
		lock.lock();
		try {
			while(count == 0)
				notEmpty.await();
			Object o = items[takeptr];
			if (++takeptr == items.length) takeptr = 0;
			--count;
			notFull.signal();
			return o;
		} finally {
			lock.unlock();
		}
	}
}
